#!/usr/bin/python

import fm,wx,tempfile,shutil,os
from base import *

def safer_save(filename,content):
	madetmp=False
	if os.path.exists(filename):
		fid,tmp=tempfile.mkstemp('__.fm-tmp-savefile',filename+'.__','.')
		os.close(fid) #else it locks the file in Windows, thanks Bill
		madetmp=True
		#print tmp
		shutil.copy2(filename,tmp)
	fd=open(filename,'wb') # has to be binary mode, I think, else the .write() may pad things out with platform eol
	fd.write(content)
	fd.close()
	# if we die anywhere above, at least tmp will remain
	if madetmp: os.remove(tmp)
	
	